<template>
  <Form ref="form" :model="data" :rules="rules" label-position="top" class="ivu-mt">
    <Row type="flex" :gutter="48">
      <Col >
        <FormItem label="原始密码" prop="oldPwd">
          <Input v-model="data.oldPwd" placeholder="请输入原始密码" />
        </FormItem>
        <FormItem label="新密码" prop="newPwd">
          <Input v-model="data.newPwd" placeholder="请输入新密码" />
        </FormItem>
        <FormItem label="确认新密码" prop="surePwd">
          <Input v-model="data.surePwd" placeholder="请再次输入新密码" />
        </FormItem>
        <FormItem>
          <Button  @click="handleSubmit">修改密码</Button>
        </FormItem>
      </Col>
    </Row>
  </Form>
</template>

<script>
import {validatorPassword} from '@/libs/validator';
import {accountResetPw} from '@/api/systemSet'
export default {
  name: "setPassword",
  data(){
    return {
      data: {
        oldPwd: '',
        newPwd: '',
        surePwd: '',
      },
      rules: {

        oldPwd: [
          { required: true, message: '请输入原始密码', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        surePwd: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    handleSubmit(){
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if(this.data.newPwd!=this.data.surePwd){
            this.$Message.warning('两次输入的密码必须保持一致')
            return
          }
          let res =await this.update();
          if(res.code==0){
            this.$Message.success('密码更新成功!');
            this.$emit('success',true)
          }else{
            this.$Message.error(_.get(res,'message')||'密码修改失败！')
          }

        }
      });
    },
   async update(){
      let {newPwd,oldPwd}= this.data
      return await accountResetPw({newPwd,oldPwd});
   },
  },
}
</script>

<style scoped>

</style>